package com.corhyfar.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.corhyfar.beans.ClienteDTO;
import com.corhyfar.beans.DetalleControlMaqDTO;
import com.corhyfar.dao.DetalleControlMaqDAO;
import com.corhyfar.util.SQLConnection;

public class DetalleControlMaqDaoImpl implements DetalleControlMaqDAO{
	
	Connection cn = null;
	PreparedStatement ps = null;
	ResultSet rs = null;

	@Override
	public DetalleControlMaqDTO obtenerDetalleControlMaq(int codigoControlMaq)
			throws SQLException {
		DetalleControlMaqDTO obtenido = null;
		cn = new SQLConnection().GetConnection();
		String sql = "select * from DETALLE_CONTROL_MAQ where COD_CMAQ=?";
		ps = cn.prepareStatement(sql);
		ps.setInt(1, codigoControlMaq);
		rs = ps.executeQuery();
		if (rs.next()) {
			obtenido = new DetalleControlMaqDTO(rs.getInt(0), rs.getInt(1), rs.getInt(2), rs.getString(3),
					rs.getString(4), rs.getString(5), rs.getString(6),
					rs.getString(7), rs.getInt(8));

		}
		ps.close();
		cn.close();
		return obtenido;
	}

	@Override
	public List<DetalleControlMaqDTO> listarDetalleControlMaq()
			throws SQLException {
		cn = new SQLConnection().GetConnection();
		String sql = "select * from CLIENTE";
		ps = cn.prepareStatement(sql);
		rs = ps.executeQuery();
		List<DetalleControlMaqDTO> lista = new ArrayList<DetalleControlMaqDTO>();
		while (rs.next()) {

			lista.add(new DetalleControlMaqDTO(rs.getInt(0), rs.getInt(1), rs
					.getInt(2), rs.getString(3), rs.getString(4), rs
					.getString(5), rs.getString(6), rs.getString(7), rs.getInt(8)));

		}
		ps.close();
		cn.close();
		return lista;
	}

	@Override
	public void actualizaDetalleControlMaq(int codigoDetalleControlMaq,
			int codigoPA, String horarioInicioAMDCMaq,
			String horarioFinAMDCMaq, String horarioInicioPMDCMaq,
			String horarioFinPMDCMaq, String totalHorasDCMaq, int codigoCliente)
			throws SQLException {
		

		
		cn = new SQLConnection().GetConnection();
		String sql = "update DETALLE_CONTROL_MAQ set COD_PA =?,HORAINIAM_DCMAQ=? ,HORAFINAM_DCMAQ =?,HORAINIPM_DCMAQ =?,HORAFINPM_DCMAQ =?,TOTALHORAS_DCMAQ =?,COD_CLI =?";
		ps = cn.prepareStatement(sql);
		ps.setInt(1, codigoPA);
		ps.setString(2, horarioInicioAMDCMaq);
		ps.setString(3, horarioFinAMDCMaq);
		ps.setString(4, horarioInicioPMDCMaq);
		ps.setString(5, horarioFinPMDCMaq);
		ps.setString(5, totalHorasDCMaq);
		ps.setInt(6, codigoCliente);
		
		int rst = ps.executeUpdate();
		
		if(rst>0){
			System.out.println("Detalle Control Modificado Modificado");
		}
		ps.close();
		cn.close();
		
				
	}
	
	@Override
	public void ingresaDetalleControlMaq(
			DetalleControlMaqDTO detalleControlMaqDTO) {
		try {
			cn = new SQLConnection().GetConnection();
			String sql="insert into DETALLE_CONTROL_MAQ values (?,?,?,?,?,?,?,?,?)";
			ps = cn.prepareStatement(sql);
			ps.setInt(1, detalleControlMaqDTO.getCodigoControlMaq());
			ps.setInt(2, detalleControlMaqDTO.getCodigoDetalleControlMaq());
			ps.setInt(3, detalleControlMaqDTO.getCodigoPA());
			ps.setString(4, detalleControlMaqDTO.getHorarioInicioAMDCMaq());
			ps.setString(5, detalleControlMaqDTO.getHorarioFinAMDCMaq());
			ps.setString(6, detalleControlMaqDTO.getHorarioInicioPMDCMaq());
			ps.setString(7, detalleControlMaqDTO.getHorarioFinPMDCMaq());
			ps.setString(8, detalleControlMaqDTO.getTotalHorasDCMaq());
			ps.setInt(9, detalleControlMaqDTO.getCodigoCliente());
						
			int rst = ps.executeUpdate();
			ps.close();
			cn.close();
			
			System.out.println("Detalle de Control Maquinaria registrada");
		
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}


	@Override
	public void eliminaDetalleControlMaq(int codigoControlMaq) {
		try {
			cn = new SQLConnection().GetConnection();
			String sql="delete from DETALLE_CONTROL_MAQ where COD_CMAQ=?";
			ps = cn.prepareStatement(sql);
			ps.setInt(1, codigoControlMaq);
			rs=ps.executeQuery();
			ps.close();
			cn.close();
			
			
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	
}
